<template>
  <div class="cinema-page-wrap">
   
    <div class="cinema-search-wrap">
      <div class="cinema-location" @click="setLoction">{{currentCity.nm}}<span class="cinema_city_change"></span></div>
      <div class="cinema-search" @click="toSearchCinema">
        <span><i class="el-icon-search"></i></span>
        <span>请输入影院</span>
      </div>
    </div>
  <!-- 筛选框 -->
  
  <tab-map :filter-cinemas="filterCinemas" :select-list="selectList" @change-brand="changeBrand"   @change-district="changeDistrict" @change-subway="changeSubway" @sumbit-btn="sumbitBtn"></tab-map>
  <Loading :is-data="isData" :is-erro="isErro" @get-data="getData" :pull-down-msg="pullDownMsg"></Loading>
  <!-- 影院列表 -->
    <cinema-list :refresh="refresh" :pull-up-msg="pullUpMsg" :cinema-list="cinemas"  class="cinema-list" @cineam-detail-handler="cineamDetailHandler" @scrolled="scrolled" @pull-up-scroll="pullUpScroll" @pull-up-end="pullUpEnd"></cinema-list>
    
  </div>
</template>
<script>
import getCinemasAxios from './getCinemasAxios.js'

import tabMap from '../../views/cinema-views/tabmap/tabmap'
import cinemaList from '../../views/cinema-views/cinemalist/cinemalist'
import Loading from '@/components/loading/loading'
import { mapState } from 'vuex'

export default {
  data () {
    return {
      isData : false,
      isErro : false,
      filterCinemas: [],
      selectList: {},
      cinemas: [ ],
      // 下拉加载
      offset: 0,
      hasMore:true,
      pullDownMsg: '',
      pullUpMsg:'',
      refresh:false
    }
  },
  components: { tabMap, cinemaList, Loading },
  computed: {
    ...mapState(['selectLists', 'currentCity'])
  },
  mounted () {
    this.selectList = this.selectLists
    this.getCinemas()

    this.axios('/ajax/filterCinemas?ci=' + this.currentCity.id).then(res => {
      this.filterCinemas = res.data
    })
    // ajax请求
  },
  methods: {
    cineamDetailHandler (item) {
      this.$router.push({ path: '/cinemaTicket', query: { cinemaId: item.id } })
    },
    setLoction () {
      this.$router.push('/cityList')
    },
    toSearchCinema () {
      this.$router.push('/cinemaSearch')
    },
    // 切换电影院事件
    changeBrand (item) {
      this.selectList['brandId'] = item.id
      // ajax请求
      this.getCinemas()
    },
    // 行政区域事件
    changeDistrict (obj) {
      this.selectList['districtId'] = obj.district.id
      this.selectList['areaId'] = obj.area ? obj.area.id : -1
      // ajax请求
      this.getCinemas()
    },
    // 切换地铁事件
    changeSubway (obj) {
      this.selectList['subwayId'] = obj.subway.id
      this.selectList['stationId'] = obj.station ? obj.station.id : -1
      // ajax请求
      this.getCinemas()
    },
    hiddentap (e) {
      this.showIndex = 0
      this.mapActive = 0
    },
    // 服务筛选事件
    sumbitBtn (item) {
      for (var key in item) {
        this.selectList[key] = item[key]
      }
      // ajax请求
      this.getCinemas()
    },
    getData() {
      this.isErro = false;
      this.getMxpectedList(this.currentCity.id)
    },
    scrolled() {
      this.refresh = false;
    },
    getCinemas (offset=0,bool=true,isPull=false) {
      this.isData = bool;
      this.$store.commit('getSelectLists', this.selectList)
      getCinemasAxios(this.selectList,offset).then(res => {
          if ( res.data.cinemas.length > 0 ) { // 有数据   
              // 下拉刷新数组重置，下拉加载数组添加
              if (isPull) {
                  this.cinemas = []
              }
              this.cinemas = this.cinemas.concat(res.data.cinemas);
              this.hasMore =  res.data.paging.hasMore;
              this.isData = false;
              this.pullUpMsg = '';
              setTimeout(() => {
                  this.pullDownMsg = '';
              }, 500);
              this.$nextTick(() => {
                this.refresh = true 
              })
          } 
      }).catch(err => {
          if( err ) {
            setTimeout(()=>{
                this.isData = false
                this.isErro = true;
            },3000)
          }
      })
    },
    pullUpScroll(obj) {
      if( -obj.pos.y > obj.scrTop ) {
          if( this.hasMore ) {
              this.pullUpMsg = '数据正在加载中';
          } else {
              this.pullUpMsg = '数据到底了';
          }
      }
      if( obj.pos.y > 0 ) {
        this.pullDownMsg = '释放更新数据';
      }
    },
    pullUpEnd(obj) {
        if( -obj.pos.y > obj.scrTop-20 ) {
            if( this.hasMore ) {
              this.offset += 20;
              this.getCinemas(this.offset,false)
            }
        }
        if( obj.pos.y > 0 ) {
          this.pullDownMsg = '已显示最新数据';
          this.getCinemas(0,false,true)
        }
    },   
  }
}
</script>
<style scoped>
.cinema-page-wrap {
  height: 100%;
  flex-grow: 1;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
}

.cinema-search-wrap {
  flex-shrink: 0;
  height: 44px;
  padding: 0 15px;
  display: flex;
  flex-direction: row;
  justify-content: space-around;
  align-items: center;
  background: #f5f5f5;
  color: #777777;
}
.cinema-location {
  flex-shrink: 0;
  font-size: 16px;
  line-height: 44px;
  color: #666666;
}
.cinema-search {
  flex-grow: 1;
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  padding: 4px 0;
  border: 1px solid #e6e6e6;
  background: white;
  color: #b2b2b2;
  border-radius: 5px;
  font-size: 14px;
  margin-left: 20px;
}
.cinema-search span:first-child{
  margin: 0 5px;
}
.cinema-search .iconfont {
  font-size: 14px;
  text-align: center;
}
.cinema_city_change{
    display: inline-block;
    margin-left: 4px;
    margin-top: 5px;
    width: 0;
    height: 0;
    border: 4px solid #b0b0b0;
    border-left-color: transparent;
    border-right-color: transparent;
    border-bottom-color: transparent;
}
.cinema-list{
  width: 100%;
  height: 100%;
  margin-bottom: 52px;
  overflow: auto;
}
</style>
